package com.example.service;

import com.example.entity.Employee;
import com.example.entity.Student;
import com.example.exception.CustomException;
import com.example.mapper.StudentMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class StudentService {

    @Resource
    private StudentMapper studentMapper;

    public List<Student> selectAll(Student student) {
        return studentMapper.selectAll(student);
    }

    //分页查询
    public PageInfo<Student> selectPage(Student student, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Student> list = studentMapper.selectAll(student);
        return PageInfo.of(list);
    }

    public void add(Student student) {
        studentMapper.insert(student);
    }

    public void update(Student student) {
        studentMapper.update(student);
    }

    public void delete(Integer id) {
        studentMapper.delete(id);
    }

    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids){
            studentMapper.delete(id);
        }
    }


    public Student login(Student student) {
        String studentName = student.getStudentName();
        Student dbstudent = studentMapper.selectByName(studentName);
        if(dbstudent == null) {
            throw new CustomException("500","名字错误");
        }
        return dbstudent;
    }
}
